// my/pages/MyConsultations/MyConsultations.js
import {
  BASE_URL
} from '../../../http/config';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0, //索引下标
    userid: '', // 用户id
    teacherAll: [],
    pageNo: 1,
    pageTotal: 1, // 总页数
    isLoading: false, // 防止重复请求

    indexPro: 0, //索引下标
    useridPro: '', // 用户id
    teacherAllPro: [],
    pageNoPro: 1,
    pageTotalPro: 1, // 总页数
    isLoadingPro: false, // 防止重复请求

    // img: BASE_URL
    img:''
  },
  changeIndex(e) {
    this.setData({
      index: e.target.dataset.index
    })
  },
  toConsult(e){
    const consultid = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/consult/pages/consultDetail/consultDetail?id=${consultid}`,
    })
  },

  // 获取用户的订单
  getOrder() {
    if (this.data.isLoading) return;
    this.setData({
      isLoading: true
    });
    wx.request({
      url: `${BASE_URL}/consultOrder/page/userId/${this.data.userid}`,
      data: {
        pageNum: this.data.pageNo
      },
      success: (res) => {
        console.log(res);
        this.setData({
          teacherAll: this.data.pageNo === 1 ? res.data.data.list : [...this.data.teacherAll, ...res.data.data.list],
          pageTotal: res.data.data.pageTotal || 1,
          isLoading: false,
        });
        if (this.data.pageNo === 1) {
          wx.stopPullDownRefresh();
          wx.hideNavigationBarLoading();
        }
      },
      fail() {
        this.setData({
          isLoading: false
        });
        wx.stopPullDownRefresh();
        wx.hideNavigationBarLoading();
      }
    })
  },
  onReachBottom() {
    if (this.data.pageNo >= this.data.pageTotal) {
      wx.showToast({
        title: '没有更多数据!',
        icon: 'none'
      })
      return;
    }
    console.log('加载下一页数据')
    this.setData({
      pageNo: this.data.pageNo + 1
    })
    this.getOrder()
  },



  // 获取用户的订单
  getOrderPro() {
    if (this.data.isLoadingPro) return;
    this.setData({
      isLoadingPro: true
    });
    wx.request({
      url: `${BASE_URL}/consultOrder/page/userIdPro/${this.data.userid}`,
      data: {
        pageNum: this.data.pageNoPro
      },
      success: (res) => {
        console.log(res);
        this.setData({
          teacherAllPro: this.data.pageNoPro === 1 ? res.data.data.list : [...this.data.teacherAllPro, ...res.data.data.list],
          pageTotalPro: res.data.data.pageTotal || 1,
          isLoadingPro: false,
        });
        if (this.data.pageNoPro === 1) {
          wx.stopPullDownRefresh();
          wx.hideNavigationBarLoading();
        }
      },
      fail() {
        this.setData({
          isLoadingPro: false
        });
        wx.stopPullDownRefresh();
        wx.hideNavigationBarLoading();
      }
    })
  },
  onReachBottomPro() {
    if (this.data.pageNoPro >= this.data.pageTotalPro) {
      wx.showToast({
        title: '没有更多数据!',
        icon: 'none'
      })
      return;
    }
    console.log('加载下一页数据')
    this.setData({
      pageNoPro: this.data.pageNoPro + 1
    })
    this.getOrderPro()
  },
  onPullDownRefreshPro() {
    console.log('上拉刷新');
    wx.showNavigationBarLoading(); // 显示顶部加载动画
    this.setData({
      pageNoPro: 1
    });
    this.getOrderPro()
  },




  onPullDownRefresh() {
    console.log('上拉刷新');
    wx.showNavigationBarLoading(); // 显示顶部加载动画
    this.setData({
      pageNo: 1
    });
    this.getOrder()
  },
  // 生命周期函数--监听页面加载
  onLoad(options) {
    const userInfo = wx.getStorageSync("userInfo")
    this.setData({
      userid: userInfo.id
    })
    this.getOrder();
    this.getOrderPro();
  },
})